package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import linter.linterjava.POJO.DO.User;
import linter.linterjava.POJO.DO.UserCard;
import linter.linterjava.POJO.DO.UserStatus;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.mapper.UserCardMapper;
import linter.linterjava.mapper.UserStatusMapper;
import linter.linterjava.service.UserCardService;
import linter.linterjava.service.UserService;
import linter.linterjava.mapper.UserMapper;
import linter.linterjava.service.UserStatusService;
import linter.linterjava.utils.JwtUtils;
import linter.linterjava.utils.MD5Utils;
import linter.linterjava.utils.PropertyUtils;
import linter.linterjava.utils.RedisUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static linter.linterjava.custom.Constant.*;
import static linter.linterjava.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【user(用户详情表)】的数据库操作Service实现
* @createDate 2024-07-07 09:53:12
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    UserMapper userMapper;
    @Resource
    UserCardMapper userCardMapper;
    @Resource
    UserStatusMapper userStatusMapper;

    @Override
    public UserDTO getUserByUsername(String username) {
        return PropertyUtils.doToDto(userMapper.getUserByUsername(username), UserDTO.class);
    }

    /**
     * 定时查看用户组
     */
    @Scheduled(cron = "10 25 13 * * ?")
    public void test() {
        List<UserDTO> listUser = listUser();
        System.out.println(listUser);
    }
    public List<UserDTO> listUser() {
        List<User> userList = userMapper.selectList(new QueryWrapper<>());
        return PropertyUtils.listToList(userList, UserDTO.class);
    }

    @Override
    public String checkLogin(String username, String pwd) {
        String encryptedPwd = MD5Utils.getMd5UserPwd(username,pwd);
        User user = userMapper.getUserByUsername(username);
        if (user == null) { return USER_NOT_EXIST; }
        if ( !encryptedPwd.equals(user.getUserPassword()) ) { return PASSWORD_ERROR; }
        if (user.getRole() ==1 ){ return USER_ERROR; }
        Date date = new Date();
        String time = date.toString();
        // 生成token
        String token = JwtUtils.creatToken(username,time);
//        System.out.println(token);
        RedisUtils.put(username,time,REFRESH_TOKEN_ACTIVE);
        return token;
    }

    @Override
    public Long register(String username, String pwd, String phone, String email) {
        User user = new User();
        Date date = new Date();
        String encryptedPwd = MD5Utils.getMd5UserPwd(username,pwd);
        user.setUserName(username);
        user.setUserPassword(encryptedPwd);
        user.setUserPhone(phone);
        user.setUserEmail(email);
        user.setCreateTime(date);
        user.setUpdataTime(date);
        Long flag = userMapper.insertThenById(user);
        UserCard userCard = new UserCard();
        userCard.setUserId(user.getId());
        userCard.setNickname(DEFAULT_USER_NAME);
        userCard.setUserImg(DEFAULT_IMG_URL_ICON);
        userCard.setBirthday(date);
        userCard.setUserIntro(DEFAULT_USER_NAME);
        userCard.setSex(DEFAULT_USER_SEX);
        userCard.setAddress(DEFAULT_USER_NAME);
        userCard.setCreateTime(date);
        userCard.setUpdataTime(date);
        flag += userCardMapper.insert(userCard);
        UserStatus userStatus = new UserStatus();
        userStatus.setUserId(user.getId());
        userStatus.setFenNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        userStatus.setAttentionNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        userStatus.setLearnTime(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        userStatus.setLearnLv(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        userStatus.setNoteNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        userStatus.setArticleNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        userStatus.setReviewsNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        userStatus.setCreateTime(date);
        userStatus.setUpdataTime(date);
        flag += userStatusMapper.insert(userStatus);
        return flag;
    }


    /**
     * 重写removeById，将其删除所需表中的所需字段
     * @param id 用户ID
     * @return 返回删除成功没
     */
    public boolean removeAllById(Long id) {
        if (userMapper.deleteById(id) == 1){
            userCardMapper.deleteById(id);
            userStatusMapper.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public Long getIdByUsername(String username) { return userMapper.getIdByUsername(username); }
}




